package com.qst.crop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qst.crop.dao.BankDao;
import com.qst.crop.dao.FinanceDao;
import com.qst.crop.dao.FinancingIntentionDao;
import com.qst.crop.entity.*;
import com.qst.crop.service.FinanceService;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.stereotype.Service;

import com.qst.crop.service.FinanceService;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.spi.http.HttpHandler;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Exchanger;

@Slf4j
@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    private FinancingIntentionDao financingIntentionDao;

    @Autowired
    private BankDao bankDao;
    /**
     * @Description : 查询银行
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.Bank>>
     */
    @Override
    public List<Bank> selectBank() {
        return bankDao.selectAll();
    }

    /**
     * @Description : 个人融资
     *  * @param finance
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void addPersonal(Finance finance) {
        financeDao.addPersonal(finance);
    }

    /**
     * @Description : 组合融资
     *  * @param finance
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void addMulti(Finance finance) {
        financeDao.addMulti(finance);
    }

    /**
     * @Description : 推荐融资人
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.RecommendVO>>
     */
    @Override
    public List<RecommendVO> selectRecommandByName(String name) {
        List<FinancingIntention> financingIntention = financingIntentionDao.selectByName(name);
        if (financingIntention!=null && financingIntention.size()>0) {
            return financingIntentionDao.selectAll(name);
        }else{
            return financingIntentionDao.selectWithNoneIntention(name);

        }
    }

    /**
     * @Description : 查询个人融资
     *  * @param
     * @Return : com.qst.crop.common.Result<java.util.List<com.qst.crop.entity.Finance>>
     */
    @Override
    public List<Finance> selectByName(String username) {
        return financeDao.selectByName(username);
    }

    /**
     * @Description : 通过Id查询个人融资
     *  * @param id
     * @Return : com.qst.crop.common.Result<com.qst.crop.entity.Finance>
     */
    @Override
    public Finance selectById(Integer id) {
        return financeDao.selectById(id);
    }

    /**
     * @Description : 删除融资
     *  * @param id
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void deleteById(Integer id) {
        financeDao.deleteById(id);
    }

    /**
     * @Description : 更新融资
     *  * @param finance
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void updateByName(Finance finance) {
        finance.setUpdateTime(LocalDateTime.now());
        financeDao.updateById(finance);
    }


    @Override
    public FinanceDetail selectFinaceUser(String username,Integer bankId){
        return financeDao.selectFinaceUser(username,bankId);
    }

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public FinanceDetail showBank(Integer bankId){
        return financeDao.showBank(bankId);
    }

    @Override
    public List<RecommendVO> selectRecommend(String name) {
        // 查询用户的贷款意向
        Intention intention = financeDao.selectIntention(name);
        if (StringUtils.isEmpty(intention)) {//无意向
            List<RecommendVO> list = null;
            System.out.println("无意向：" + list);
            return list;
        } else {//有意向
            System.out.println("有意向");
            // 设置远程 API 的 URL
            String apiURL = "http://127.0.0.1:5000/match/" + name;
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            // 构造请求参数
            Map<String, Object> requestParam = new HashMap<>();
            HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers);
            // 发送 GET 请求到远程 API
            ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.GET, request, String.class);
            // 从响应中解析出推荐的用户名集合
            Set<String> nameSet = JSONObject.parseObject(entity2.getBody()).getJSONObject("data").keySet();
            String[] names = nameSet.toArray(new String[0]);

            // 调用 DAO 层查询推荐数据
            List<RecommendVO> recommends = financeDao.selectRecommend(names);
            for (RecommendVO recommend : recommends) {
                System.out.println(recommend);
            }
            return recommends;
        }

    }

    @Override
    public List<Integer> getAllBid(String username){
        return financeDao.getAllBid(username);
    }

}
